package com.magic.gre.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.magic.gre.R;
import com.magic.gre.entity.OptionBean;
import com.magic.gre.helper.Contract;
import com.magic.gre.ui.activity.WebActivity;
import com.magic.gre.widget.BannerRecyclerView;
import com.noname.lib_base_java.adapter.CommonAdapter;
import com.noname.lib_base_java.adapter.ViewHolder;

import java.util.List;

/**
 * Author: Lmoumou
 * Date: 2019-01-14 14:32
 * 文件名: HomeAdapter
 * 描述:
 */
public class HomeAdapter extends CommonAdapter<String> {
    private OnHomeAdapterListener onHomeAdapterListener;
    private OptionBean optionBean;

    public void setOnHomeAdapterListener(OnHomeAdapterListener onHomeAdapterListener) {
        this.onHomeAdapterListener = onHomeAdapterListener;
    }

    public HomeAdapter(Context mContext, List<String> mData) {
        super(mContext, mData);
    }

    public void setOption(OptionBean optionBean) {
        this.optionBean = optionBean;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mInflater.inflate(viewType, parent, false));
    }

    @Override
    protected void bindData(final ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case R.layout.item_home_0:
                if (onHomeAdapterListener != null) {
                    onHomeAdapterListener.onInitViewPaher((BannerRecyclerView) holder.getView(R.id.banner_rcy));
                }
                break;
            case R.layout.item_home_1:
                holder.getView(R.id.new_words_tv).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onHomeAdapterListener!=null){
                            onHomeAdapterListener.onItemClick( holder.getView(R.id.new_words_tv));
                        }

                    }
                });
                break;
            case R.layout.item_home_2:
                holder.getView(R.id.ripe_words_tv).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onHomeAdapterListener!=null){
                            onHomeAdapterListener.onItemClick( holder.getView(R.id.ripe_words_tv));
                        }
//
                    }
                });
                break;
            case R.layout.item_home_3:
                holder.getView(R.id.count_tv).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onHomeAdapterListener!=null){
                            onHomeAdapterListener.onItemClick( holder.getView(R.id.count_tv));
                        }
//
                    }
                });

                break;
            case R.layout.item_home_4:
                if (optionBean != null) {
                    holder.setText(R.id.option_tv, optionBean.getOption());
                    holder.getView(R.id.option_tv).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            WebActivity.startThis(mContext, optionBean.getOption(), Contract.GROOM_URL);
                        }
                    });
                }
                break;
        }
    }

    @Override
    public int getItemCount() {
        return 5;
    }

    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case 0:
                return R.layout.item_home_0;
            case 1:
                return R.layout.item_home_1;
            case 2:
                return R.layout.item_home_2;
            case 3:
                return R.layout.item_home_3;
            case 4:
                return R.layout.item_home_4;
            default:
                return -1;
        }
    }

    public interface OnHomeAdapterListener {
        void onInitViewPaher(BannerRecyclerView bannerRcy);

        void onItemClick(View view);
    }
}
